package com.jxstyle.modular.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jxstyle.core.base.BaseController;
import com.jxstyle.core.exception.BizException;
import com.jxstyle.core.exception.BizExceptionEnum;
import com.jxstyle.modular.entity.Menu;
import com.jxstyle.modular.service.MenuService;

@Controller
@RequestMapping("/menu")
public class MenuController extends BaseController {

	@Autowired
	private MenuService menuService;

	@GetMapping(value = "")
	public String menupage() {
		return "/menu/list";
	}

	@GetMapping(value = "/add")
	public String add() {
		return "/menu/add";
	}

	@GetMapping(value = "/edit/{id}")
	public String edit(@PathVariable String id, ModelMap modelMap) {
		modelMap.put("id", id);
		return "/menu/edit";
	}

	@GetMapping(value = "/tree")
	public String tree() {
		return "/menu/tree";
	}

	@ResponseBody
	@PostMapping(value = "/add")
	public Boolean add(Menu menu) {
		menuService.save(menu);
		return true;
	}

	@ResponseBody
	@PostMapping(value = "/edit")
	public Boolean edit(Menu menu) {
		menuService.updateById(menu);
		return true;
	}

	@ResponseBody
	@PostMapping(value = "/delete/{id}")
	public Boolean delete(@PathVariable String id) {
		menuService.removeById(id);
		return true;
	}

	@ResponseBody
	@GetMapping(value = "/get/{id}")
	public Menu get(@PathVariable String id) {
		Menu menu = menuService.getById(id);
		Menu pmenu = menuService.getById(menu.getPid());
		if (pmenu != null) {
			menu.setPname(pmenu.getName());
		} else {
			menu.setPname("顶级");
		}
		return menu;
	}

	@ResponseBody
	@PostMapping(value = "/checkcode")
	public Boolean add(String code, String id) {
		if (id != null && !"".equals(id)) {
			Menu menu = menuService.getById(id);
			if (menu.getCode().equals(code)) {
				return true;
			}
		}
		if (menuService.getByCode(code) != null) {
			throw new BizException(BizExceptionEnum.DATA_REPEAT_ERROR);
		}
		return true;
	}

	@ResponseBody
	@PostMapping(value = "/tree")
	public List<Menu> menuTree(Boolean ishHaveTop, Integer ismenu) {
		QueryWrapper<Menu> wrapper = new QueryWrapper<>();
		wrapper.eq(ismenu != null, "ismenu", ismenu);
		wrapper.orderByAsc("sort");
		List<Menu> list = menuService.list(wrapper);
		Menu menu = menuService.convertTree(list);
		if (ishHaveTop != null && ishHaveTop) {
			List<Menu> resultList = new ArrayList<>();
			resultList.add(menu);
			return resultList;
		}
		return menu.getChilds();
	}

	@ResponseBody
	@PostMapping(value = "/list")
	public List<Menu> menu(Integer pid) {
		List<Menu> list = new ArrayList<>();
		if (pid == null) {
			pid = 0;
		}
		QueryWrapper<Menu> wrapper = new QueryWrapper<>();
		wrapper.eq("pid", pid);
		wrapper.orderByAsc("sort");
		List<Menu> childs = menuService.list(wrapper);
		Menu current = menuService.getById(pid);
		String pname = menuService.getParentName(pid);
		if (current != null) {
			current.setPname(menuService.getParentName(current.getPid()));
			list.add(current);
		}
		for (Menu menu : childs) {
			menu.setPname(pname);
		}
		list.addAll(childs);
		return list;
	}

	

}
